<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-button v-hasPermi="['admin','sms:coupon:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column prop="id" header-align="center" align="center" label="id" />
        <el-table-column prop="couponType" header-align="center" align="center" label="优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" />
        <el-table-column prop="couponImg" header-align="center" align="center" label="优惠券图片" />
        <el-table-column prop="couponName" header-align="center" align="center" label="优惠卷名字" />
        <el-table-column prop="num" header-align="center" align="center" label="数量" />
        <el-table-column prop="amount" header-align="center" align="center" label="金额" />
        <el-table-column prop="perLimit" header-align="center" align="center" label="每人限领张数" />
        <el-table-column prop="minPoint" header-align="center" align="center" label="使用门槛" />
        <el-table-column prop="startTime" header-align="center" align="center" label="开始时间" />
        <el-table-column prop="endTime" header-align="center" align="center" label="结束时间" />
        <el-table-column prop="useType" header-align="center" align="center" label="使用类型[0->全场通用；1->指定分类；2->指定商品]" />
        <el-table-column prop="note" header-align="center" align="center" label="备注" />
        <el-table-column prop="publishCount" header-align="center" align="center" label="发行数量" />
        <el-table-column prop="useCount" header-align="center" align="center" label="已使用数量" />
        <el-table-column prop="receiveCount" header-align="center" align="center" label="领取数量" />
        <el-table-column prop="enableStartTime" header-align="center" align="center" label="可以领取的开始日期" />
        <el-table-column prop="enableEndTime" header-align="center" align="center" label="可以领取的结束日期" />
        <el-table-column prop="code" header-align="center" align="center" label="优惠码" />
        <el-table-column prop="memberLevel" header-align="center" align="center" label="可以领取的会员等级[0->不限等级，其他-对应等级]" />
        <el-table-column prop="publish" header-align="center" align="center" label="发布状态[0-未发布，1-已发布]" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip :enterable="false" effect="dark" content="编辑" placement="top">
              <el-button v-hasPermi="['admin','sms:coupon:edit']" type="success" icon="el-icon-edit" size="mini" @click="editBtn(scope.row.id)" />
            </el-tooltip>
            <el-tooltip :enterable="false" effect="dark" content="删除" placement="top">
              <el-button v-hasPermi="['admin','sms:coupon:del']" type="danger" icon="el-icon-delete" size="mini" @click="delBtn(scope.row.id)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="id" prop="id">
            <el-input v-model="addForm.id" />
          </el-form-item>
          <el-form-item label="优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" prop="couponType">
            <el-input v-model="addForm.couponType" />
          </el-form-item>
          <el-form-item label="优惠券图片" prop="couponImg">
            <el-input v-model="addForm.couponImg" />
          </el-form-item>
          <el-form-item label="优惠卷名字" prop="couponName">
            <el-input v-model="addForm.couponName" />
          </el-form-item>
          <el-form-item label="数量" prop="num">
            <el-input v-model="addForm.num" />
          </el-form-item>
          <el-form-item label="金额" prop="amount">
            <el-input v-model="addForm.amount" />
          </el-form-item>
          <el-form-item label="每人限领张数" prop="perLimit">
            <el-input v-model="addForm.perLimit" />
          </el-form-item>
          <el-form-item label="使用门槛" prop="minPoint">
            <el-input v-model="addForm.minPoint" />
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-input v-model="addForm.startTime" />
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-input v-model="addForm.endTime" />
          </el-form-item>
          <el-form-item label="使用类型[0->全场通用；1->指定分类；2->指定商品]" prop="useType">
            <el-input v-model="addForm.useType" />
          </el-form-item>
          <el-form-item label="备注" prop="note">
            <el-input v-model="addForm.note" />
          </el-form-item>
          <el-form-item label="发行数量" prop="publishCount">
            <el-input v-model="addForm.publishCount" />
          </el-form-item>
          <el-form-item label="已使用数量" prop="useCount">
            <el-input v-model="addForm.useCount" />
          </el-form-item>
          <el-form-item label="领取数量" prop="receiveCount">
            <el-input v-model="addForm.receiveCount" />
          </el-form-item>
          <el-form-item label="可以领取的开始日期" prop="enableStartTime">
            <el-input v-model="addForm.enableStartTime" />
          </el-form-item>
          <el-form-item label="可以领取的结束日期" prop="enableEndTime">
            <el-input v-model="addForm.enableEndTime" />
          </el-form-item>
          <el-form-item label="优惠码" prop="code">
            <el-input v-model="addForm.code" />
          </el-form-item>
          <el-form-item label="可以领取的会员等级[0->不限等级，其他-对应等级]" prop="memberLevel">
            <el-input v-model="addForm.memberLevel" />
          </el-form-item>
          <el-form-item label="发布状态[0-未发布，1-已发布]" prop="publish">
            <el-input v-model="addForm.publish" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="id" prop="id">
            <el-input v-model="editForm.id" />
          </el-form-item>
          <el-form-item label="优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" prop="couponType">
            <el-input v-model="editForm.couponType" />
          </el-form-item>
          <el-form-item label="优惠券图片" prop="couponImg">
            <el-input v-model="editForm.couponImg" />
          </el-form-item>
          <el-form-item label="优惠卷名字" prop="couponName">
            <el-input v-model="editForm.couponName" />
          </el-form-item>
          <el-form-item label="数量" prop="num">
            <el-input v-model="editForm.num" />
          </el-form-item>
          <el-form-item label="金额" prop="amount">
            <el-input v-model="editForm.amount" />
          </el-form-item>
          <el-form-item label="每人限领张数" prop="perLimit">
            <el-input v-model="editForm.perLimit" />
          </el-form-item>
          <el-form-item label="使用门槛" prop="minPoint">
            <el-input v-model="editForm.minPoint" />
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-input v-model="editForm.startTime" />
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-input v-model="editForm.endTime" />
          </el-form-item>
          <el-form-item label="使用类型[0->全场通用；1->指定分类；2->指定商品]" prop="useType">
            <el-input v-model="editForm.useType" />
          </el-form-item>
          <el-form-item label="备注" prop="note">
            <el-input v-model="editForm.note" />
          </el-form-item>
          <el-form-item label="发行数量" prop="publishCount">
            <el-input v-model="editForm.publishCount" />
          </el-form-item>
          <el-form-item label="已使用数量" prop="useCount">
            <el-input v-model="editForm.useCount" />
          </el-form-item>
          <el-form-item label="领取数量" prop="receiveCount">
            <el-input v-model="editForm.receiveCount" />
          </el-form-item>
          <el-form-item label="可以领取的开始日期" prop="enableStartTime">
            <el-input v-model="editForm.enableStartTime" />
          </el-form-item>
          <el-form-item label="可以领取的结束日期" prop="enableEndTime">
            <el-input v-model="editForm.enableEndTime" />
          </el-form-item>
          <el-form-item label="优惠码" prop="code">
            <el-input v-model="editForm.code" />
          </el-form-item>
          <el-form-item label="可以领取的会员等级[0->不限等级，其他-对应等级]" prop="memberLevel">
            <el-input v-model="editForm.memberLevel" />
          </el-form-item>
          <el-form-item label="发布状态[0-未发布，1-已发布]" prop="publish">
            <el-input v-model="editForm.publish" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getCoupon, listCoupon } from '@/api/coupon/coupon'

export default {
  name: 'Coupon',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      listForm: [],
      addForm: {
        id: '',
        couponType: '',
        couponImg: '',
        couponName: '',
        num: '',
        amount: '',
        perLimit: '',
        minPoint: '',
        startTime: '',
        endTime: '',
        useType: '',
        note: '',
        publishCount: '',
        useCount: '',
        receiveCount: '',
        enableStartTime: '',
        enableEndTime: '',
        code: '',
        memberLevel: '',
        publish: ''
      },
      editForm: {},
      addFormRules: {
        id: [
          { required: true, message: '请输入id', trigger: 'blur' }
        ],
        couponType: [
          { required: true, message: '请输入优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]', trigger: 'blur' }
        ],
        couponImg: [
          { required: true, message: '请输入优惠券图片', trigger: 'blur' }
        ],
        couponName: [
          { required: true, message: '请输入优惠卷名字', trigger: 'blur' }
        ],
        num: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        amount: [
          { required: true, message: '请输入金额', trigger: 'blur' }
        ],
        perLimit: [
          { required: true, message: '请输入每人限领张数', trigger: 'blur' }
        ],
        minPoint: [
          { required: true, message: '请输入使用门槛', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请输入开始时间', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '请输入结束时间', trigger: 'blur' }
        ],
        useType: [
          { required: true, message: '请输入使用类型[0->全场通用；1->指定分类；2->指定商品]', trigger: 'blur' }
        ],
        note: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ],
        publishCount: [
          { required: true, message: '请输入发行数量', trigger: 'blur' }
        ],
        useCount: [
          { required: true, message: '请输入已使用数量', trigger: 'blur' }
        ],
        receiveCount: [
          { required: true, message: '请输入领取数量', trigger: 'blur' }
        ],
        enableStartTime: [
          { required: true, message: '请输入可以领取的开始日期', trigger: 'blur' }
        ],
        enableEndTime: [
          { required: true, message: '请输入可以领取的结束日期', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入优惠码', trigger: 'blur' }
        ],
        memberLevel: [
          { required: true, message: '请输入可以领取的会员等级[0->不限等级，其他-对应等级]', trigger: 'blur' }
        ],
        publish: [
          { required: true, message: '请输入发布状态[0-未发布，1-已发布]', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化列表
    this.initListCoupon()
  },

  methods: {
    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListCoupon()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getCoupon(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListCoupon()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListCoupon()
          }
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListCoupon()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListCoupon()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 初始化数据
    initListCoupon() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listCoupon(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
